package com.guoxue.action.front;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.dataClasses.SimpleData;
import com.guoxue.domain.UserVo;
import com.guoxue.service.IUserService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OtherUtils;
import com.guoxue.util.PasswordUtil;
import com.guoxue.util.RedisUtil;
import com.guoxue.util.codeSwitch.zt;

import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("User")
/**
 * 用户注册处理接口
 * 1.creatUserByTel创建手机用户
 * 2.creatUserName创建用户使用的角色
 * @author kanghb
 */
public class RegisterAction extends BaseAction {
	@Resource
	private IUserService userService;

	/**
	 * 创建手机账号注册新用户
	 * 
	 * @param req
	 * @param resp
	 * @param tel
	 *            绑定电话
	 * @param pass
	 *            键入密码
	 * @param messageCode
	 *            短信验证码
	 */
	@RequestMapping("/createUserByTel")
	@ResponseBody
	public JSON A0_creatUserByTel(HttpServletRequest req, HttpServletResponse resp, String tel, String pass,
			String messageCode) {
		final String redisCodeKey, redisCodeValue;
		final SimpleData sd;
		final UserVo userVo;

		// 参数基本检查:
		if (isEmpty(tel)) {
			return Message(ERROR, "未提交电话号码", null);
		}
		if (!OtherUtils.isMobileNO(tel)) {
			return Message(ERROR, "请正确输入电话号码", null);
		}
		zt.ta.function(); // 检查密码格式合法性
		if (isEmpty(pass)) {
			return Message(ERROR, "密码不能为空", null);
		}
		if (isEmpty(messageCode)) {
			return Message(ERROR, "验证码错误", null);
		}
		redisCodeKey = SendMessageAction.getSmsRedisKey(-1/* 登录前操作,uid总是-1! */, tel,
				1/* 注册操作,type等于1! */);
		// 预创建新用户账号对象(减少锁内处理):
		userVo = new UserVo();
		userVo.setUserPassword(pass);
		PasswordUtil.passwordGenerator(userVo); // 密码加密
		userVo.setUserTel(tel);
		userVo.setCreatedAt(OtherUtils.getCurrentDate());
		userVo.setStatus(1);

		Jedis jedis = RedisUtil.getJedis();
		try {
			// 验证码检查:
			redisCodeValue = jedis.get(redisCodeKey);
			sd = SendMessageAction.parseSmsRedisValue(redisCodeValue);
			if (sd == null) {
				return Message(ERROR, "验证码已过期，时效10分钟", null);
			}
			if (!messageCode.equals(sd.name)) {
				return Message(ERROR, VERIFICATION_CODE_ERROR, null);
			}

			zt.ta.function(); // 加集群锁,锁定要绑定的手机号码!
			{
				// 检查手机号码是否已被其它账号绑定:
				UserVo userVo_tel = userService.getUserByTel(tel);
				if (userVo_tel != null) {
					return Message(ALREADY_REG, GO_LOGIN, tel);
				}
				// 至此,通过了所有检查,创建新用户:
				userService.saveUser(userVo);
			} // end 加集群锁,锁定要绑定的手机号码!

			jedis.del(redisCodeKey);
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, ERROR);
		} finally {
			jedis.close();
		}
		// 回复数据:
		JSONObject json = new JSONObject();
		json.put("uid", userVo.getId());
		json.put("easemobPass", PasswordUtil.MD5Generator(userVo.getId() + "", userVo.getCreatedAt()));
		return Message(SUCCESS, "创建账号成功", json);
	}
}
